package com.xiaotu.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

@Service
public class SendMailService {
    @Autowired
    private JavaMailSender javaMailSender;
    @Value("${spring.mail.username}")
    private String from;
    /**
     * from 发件人地址
     * to 收件人地址
     * subject 邮件标题
     * text 邮件内容
     * @param to
     * @param subject
     * @param text
     */
    public void sendSimpleEmail(String to,String subject,String text){
        //定制纯文本邮件消息SimpleMailMessage
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        mailMessage.setFrom(from);
        mailMessage.setTo(to);
        mailMessage.setSubject(subject);
        mailMessage.setText(text);
        try {
            javaMailSender.send(mailMessage);
            System.out.println("纯文本邮件发送成功");
        }catch (MailException e){
            System.out.println("纯文本邮件发送失败"+e.getMessage());
            e.printStackTrace();
        }
    }
    public void sendComplexEmail(String to , String subject,String text,String filePath,String rscId,String rscPath){
        //定制复杂邮件信息
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        try{
            //使用MimeMessageHelper帮助类,并设置multipart多部件为true
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
            helper.setFrom(from);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(text,true);
            //设置邮件静态资源
            FileSystemResource res = new FileSystemResource(new File(rscPath));
            helper.addInline(rscId, res);
            //设置邮件附件
            FileSystemResource file = new FileSystemResource(new File(filePath));
            String fileName = filePath.substring(filePath.lastIndexOf(File.separator));
            helper.addAttachment(fileName,file);
            //发送邮件
            javaMailSender.send(mimeMessage);
            System.out.println("复杂邮件发送成功");
        }catch (MessagingException e){
            System.out.println("纯文本邮件发送失败"+e.getMessage());
            e.printStackTrace();
        }
    }
    public void sendTemplateEmail(String to,String subject,String content){
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        try{
            //使用MimeMessageHelper 帮助类,并设置multipart多部件为true
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
            helper.setFrom(from);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(content,true);
            //发送邮件
            javaMailSender.send(mimeMessage);
            System.out.println("发送模板邮件成功");
        }catch (Exception e){
            System.out.println("发送模板邮件失败"+e.getMessage());
            e.printStackTrace();
        }
    }
}
